import send_msg from '@feidao-factory/send-msg';
import config from '@feidao-factory/send-msg/nodejs/config';

const QQ_LOGIN = 'thirdpart.qq.gettokenanduserinfo';
const THIRD_LOGIN = 'thirdpart.login';

export default async function login(code: string, actionid: string, sessionid: string, logintype: 'qq_pc' | 'qq_phone') {
	const spaceid = config.spaceid;
	const token = await send_msg<{
		access_token: string;
		expires_in: string;
		refresh_token: string;
	}>(actionid, sessionid, QQ_LOGIN, {
		code,
		flag: '101',
		granttype: 'authorization_code',
		logintype,
		spaceid
	});
	const data = await send_msg<{
		_id: string;
		usercode: string;
		user_name: string;
		name_spell: string;
		is_disabled: boolean;
		create_time: number;
		productid: string;
		avatar_path: string;
		avatar_name: string;
		avatar_type: string;
		email: string;
		phone: string;
		last_login_time: number;
		phone_verified: boolean;
		email_verified: boolean;
		is_admin: string;
		code: number;
		msg: string;
		sessionID: string;
	}>(actionid, sessionid, THIRD_LOGIN, {
		accesstoken: token.access_token,
		flag: '102',
		logintype,
		spaceid
	});
	return { cookie: { sessionid: data.sessionID }, data };
}
